/*
 * User: rant
 * Date: 26/02/2007
 *
 *
 */
package laMalinche.rete;

import laMalinche.rete.nodes.ActionHolder;
import laMalinche.rete.nodes.ConditionNode;
import laMalinche.rete.rule.Action;
import laMalinche.rete.rule.Condition;


import org.jetbrains.annotations.NotNull;

/**
 *
 */
public class BusinessToProduction {
	protected ConditionNode getNewConditionNode(Object condition, Class<? extends ConditionNode> nodeType) {
		ConditionNode conditionNode = noExceptionsNewInstance(nodeType);
		if (condition instanceof Action) {
			final ActionHolder actionHolder = ((ActionHolder) conditionNode);
			actionHolder.setAction((Action) condition);
			actionHolder.setTrigger(true);
		} else {
			conditionNode.setCondition((Condition) condition);
		}
		return conditionNode;
	}

	protected <T> T noExceptionsNewInstance(@NotNull Class<T> clazz) {
		T result;
		try {
			result = clazz.newInstance();
		} catch (InstantiationException e) {
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e);
		}
		return result;
	}
}